package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.constrant.ProductConst;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void deleteRelation(List<AttrAttrgroupRelationEntity> relationEntities) {

        this.baseMapper.deleteBatchRelation(relationEntities);
    }

    @Override
    public void addRelation(List<AttrAttrgroupRelationEntity> relationEntityList) {
        this.baseMapper.saveBatchRelation(relationEntityList);
    }

    @Override
    public List<AttrGroupWithAttrsVo> queryAttrGroupWithAttrs(Long catelogId) {
        List<AttrGroupWithAttrsVo> vos = new ArrayList<>();
        //分类查分组
        List<AttrGroupEntity> attrGroupEntityList = this.attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<Long> attrGroupIds = attrGroupEntityList.parallelStream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        //分组查组与属性的关联
        List<AttrAttrgroupRelationEntity> attrGroupRelationAttrList = this.baseMapper.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroupIds));
        //将组与属性的结果集进行分组
        Map<Long, List<Long>> listMap = attrGroupRelationAttrList.parallelStream().collect(Collectors.groupingBy(AttrAttrgroupRelationEntity::getAttrGroupId, Collectors.mapping(AttrAttrgroupRelationEntity::getAttrId, Collectors.toList())));
        //收集所有属性id
        List<Long> attrIds = attrGroupRelationAttrList.parallelStream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        //查属性且是基本属性
        List<AttrEntity> attrEntityList = this.attrDao.selectByIdsAndAttrType(attrIds, ProductConst.AttrEnum.ARRE_TYPE_BASE.getCode());
        // 此处应该查的是 基本属性  lambda表达式引用的局部变量必须是最终变量，此处过滤后收集到的数据不能赋值给原引用变量
        // List<AttrEntity> attrEntityList = this.attrDao.selectBatchIds(attrIds);
        // attrEntityList = attrEntityList.parallelStream().filter(item -> item.getAttrType() == ProductConst.AttrEnum.ARRE_TYPE_BASE.getCode()).collect(Collectors.toList());

        //组装数据
        attrGroupEntityList.forEach(item -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, vo);
            attrEntityList.forEach(value -> {
                if (listMap.get(item.getAttrGroupId()).contains(value.getAttrId())) {
                    AttrVo attrVo = new AttrVo();
                    BeanUtils.copyProperties(value, attrVo);
                    vo.getAttrs().add(attrVo);
                }
            });
            vos.add(vo);
        });

        return vos;
    }

}